package com.exam.service.impl;

import com.exam.mapper.*;
import com.exam.po.*;
import com.exam.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XueWanglin
 * @ClassName StudentServiceImpl
 * @Description TODO
 * @Date 2021/1/11 20:20
 * @Version 1.0
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired(required = false)
    private StudentMapper studentMapper;
    @Autowired(required = false)
    private ScoreMapper scoreMapper;
    @Autowired(required = false)
    private MessageMapper messageMapper;
    @Autowired(required = false)
    private ReplayMapper replayMapper;
    @Autowired(required = false)
    private ExamManageMapper examManageMapper;
    @Autowired(required = false)
    private ExamMessageMapper examMessageMapper;
    @Autowired(required = false)
    private PaperMessageMapper paperMessageMapper;

    /**
     * 根据学生id页码和页面数据容量来查询成绩列表
     * @param page 页码
     * @param pagesize 页面数据容量
     * @param studentId 学生id
     * @return 成绩列表
     */
    @Override
    public List<Score> getScoresByStudentId(Integer page, Integer pagesize, Integer studentId) {
//        ScoreExample scoreExample = new ScoreExample();
//        ScoreExample.Criteria criteria = scoreExample.createCriteria();
//        criteria.andStudentidEqualTo(studentId);
//        List<Score> list = scoreMapper.selectByExample(scoreExample);
        List<Score> list = scoreMapper.selectByStudentId((page-1)*pagesize,pagesize,studentId);
        return list;
    }

    /**
     * 根据学生id来修改密码
     * @param student 只包含学生id和新密码的实体
     * @return 返回是否成功
     */
    @Override
    public boolean updatePwdByStudentId(Student student) {
        int result = studentMapper.updateByPrimaryKeySelective(student);
        return result>0;
    }

    /**
     * 分页查询小论坛
     * @param page 页码
     * @param pagesize 每页数据容量
     * @return
     */
    @Override
    public List getMessagesList(Integer page, Integer pagesize) {
        List<Message> list = messageMapper.selectByPageAndPagesize((page-1)*pagesize,pagesize);
        if (list!=null){
            for (Message message: list) {
                ReplayExample replayExample = new ReplayExample();
                ReplayExample.Criteria criteria = replayExample.createCriteria();
                criteria.andMessageidEqualTo(message.getId());
                List<Replay> list1 = replayMapper.selectByExample(replayExample);
                message.setList(list1);
            }
        }
        return list;
    }

    /**
     * 添加回复
     * @param replay 回复实体
     * @return 是否成功
     */
    @Override
    public Boolean addReplay(Replay replay) {
        Boolean result = replayMapper.insertSelective(replay)>0;
        return result;
    }

    /**
     * 分页查询考试数据
     * @param page 要查询的页码
     * @param pagesize 每页数据容量
     * @return 考试数据列表
     */
    @Override
    public List<ExamMessage> getExams(Integer page, Integer pagesize) {
        List<ExamMessage> list = examMessageMapper.selectByPageAndPagesize((page-1)*pagesize,pagesize);
        return list;
    }

    /**
     * 通过关键字查询考试信息
     * @param source 关键字
     * @return 考试信息
     */
    @Override
    public List<ExamMessage> getOneExamByKey(String source) {
        List<ExamMessage> list = examMessageMapper.getOneExamByKey(source);
        return list;
    }

    /**
     * 添加小论坛信息
     * @param message message实体对象
     * @return 是否成功
     */
    @Override
    public Boolean insertNewMessage(Message message) {
        Boolean flag = messageMapper.insertSelective(message)>0;
        return flag;
    }

    /**
     * 通过examCode查询考试详细信息
     * @param examCode 要查询的考试编号
     * @return
     */
    @Override
    public ExamMessage getOneExamMessageByExamCode(Integer examCode) {
        ExamMessage examMessage = examMessageMapper.getOneExamMessageByExamCode(examCode);
        return examMessage;
    }

    /**
     * 通过paperId获取试卷详细内容
     * @param paperId 要获取的试卷Id
     * @return 试卷内容集合
     */
    @Override
    public List<List<PaperMessage>> getPaperAndQueByPaperId(Integer paperId) {
        List<List<PaperMessage>> list = new ArrayList<>();
        List<PaperMessage> listM = paperMessageMapper.getPaperAndMultiQueByPaperId(paperId);
//        for (PaperMessage paperMessage: listM){
//            list.add(paperMessage);
//        }
        list.add(listM);
        List<PaperMessage> listF = paperMessageMapper.getPaperAndFillQueByPaperId(paperId);
//        for (PaperMessage paperMessage: listF){
//            list.add(paperMessage);
//        }
        list.add(listF);
        List<PaperMessage> listJ = paperMessageMapper.getPaperAndJudgeQueByPaperId(paperId);
//        for (PaperMessage paperMessage: listJ){
//            list.add(paperMessage);
//        }
        list.add(listJ);
        return list;
    }

    /**
     * 获取考试数目
     * @return
     */
    @Override
    public int getExamsCount() {
        int result = examManageMapper.countAll();
        return result;
    }

    /**
     * 获取论坛讨论数目
     * @return
     */
    @Override
    public int getMessagesCount() {
        int result = messageMapper.countAll();
        return result;
    }

    /**
     * 获取分数数目
     * @return
     */
    @Override
    public int getScoresCountById(Integer studentId) {
        ScoreExample scoreExample = new ScoreExample();
        ScoreExample.Criteria criteria = scoreExample.createCriteria();
        criteria.andStudentidEqualTo(studentId);
        int result = scoreMapper.countByExample(scoreExample);
        return result;
    }

    /**
     * 学生提出异议
     * @return 是否成功
     */
    @Override
    public Boolean updateScoreStatusByScoreId(Score score) {
        Boolean flag = scoreMapper.updateByPrimaryKeySelective(score)>0;
        return flag;
    }

    @Override
    public Boolean insertNewTestResult(Score score) {
        Boolean flag = scoreMapper.insertSelective(score)>0;
        return flag;
    }

    /**
     * 模糊查询结果数
     * @param source 关键字
     * @return
     */
    @Override
    public int getExamsCountByKey(String source) {
        int result = examMessageMapper.getExamsCountByKey(source);
        return result;
    }
}
